package layers;


import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;

import annotations.MainConstructor;
import annotations.ParameterType;

import tool.animation.DiscreteAnimation;
import tool.dialog.ErrorDialog;
import mapelements.Renderer;

/** Renderer del Maplayer Grass.
 * @author Daniele
 * @version 1.0 */
public class GrassRenderer extends Renderer {

	private DiscreteAnimation animation;
	
	@MainConstructor(descriptions = { "componente contenente il background" }, types = {ParameterType.COMPONENT})
	public GrassRenderer(JComponent parent){
		super();
		try{
			this.animation = new DiscreteAnimation(null, 500);
			this.animation.addFrame(ImageIO.read(new File("fs_test/layers/GrassA.png")));
			this.animation.addFrame(ImageIO.read(new File("fs_test/layers/GrassB.png")));
			this.setDrawingComponent(parent);
			this.animation.start();
		} catch (IOException error){
			new ErrorDialog(null,"error","impossibile trovare le immagini!");
		}
	}
	
	@Override
	public BufferedImage drawRenderer(Graphics g) {
		return this.animation.paint(g);
	}

	@Override
	public GrassRenderer clone() throws CloneNotSupportedException {
		GrassRenderer cloned=(GrassRenderer) super.clone();
		cloned.animation=this.animation.clone();
		return cloned;
	}

	@Override
	public void setDrawingComponent(Component parent) {
		this.animation.setParent(parent);
	}
	
	@Override
	public void start(){
		this.animation.start();
	}
	
	@Override
	public void terminate(){
		this.animation.stop();
	}
}
